﻿using System.Data.SqlClient;
using System.Web;
using Jayden.Dll.Web;
using Jayden.Dll.Web.Controls;
using Jayden.Dll.Web.HtmlControls;

namespace Jayden.Site.BvD.Dll
{
	public class BvDPageHtml : PageHtml
	{

		private HttpContext m_Context;
		private System.Data.DataTable m_Products;

		public System.Data.DataTable Products
		{
			get
			{
				if(m_Products == null)
				{
					if (m_Context != null)
						m_Products = m_Context.Cache["Products"] as System.Data.DataTable;
					if (m_Products == null)
					{
						lock (typeof(BvDPageHtml))
						{
							if (m_Products == null)
							{
								using (SqlConnection connection = new SqlConnection(string.Format("Data Source={0};Initial Catalog=FinUsers;Connect Timeout=8;UID=dba;PWD=sql", "sqltest.be.bvd")))
								{
									connection.Open();
									SqlCommand command = connection.CreateCommand();
									command.CommandText = "SELECT * FROM Products";
									SqlDataReader reader = command.ExecuteReader();
									System.Data.DataTable dataTable = new System.Data.DataTable();
									dataTable.Load(reader);
									m_Context.Cache["Products"] = m_Products = dataTable;
								}
							}
						}
					}
				}
				return m_Products;
			}
		}

		private Div m_Content;

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader)
		{
			base.Load(node, loader);

			m_Content = GetElementByServerId<Div>("content");
			m_Content.Id = m_Content.ServerId;
		}

		public override void ProcessCssRequest(HttpHandlerContext context)
		{
			m_Context = context.Context;

			context.Css.NewElement(".mouseOver").AddProperty("background-color", "#fff");
			context.Css.NewElement("input[type=\"search\"]").AddProperty("width", "300px");

			base.ProcessCssRequest(context);
		}

		public override void ProcessJsRequest(HttpHandlerContext context)
		{
			m_Context = context.Context;
			base.ProcessJsRequest(context);

		}

		public override void ProcessJsonRequest(HttpHandlerContext context)
		{
			m_Context = context.Context;
			base.ProcessJsonRequest(context);
		}

		public override void ProcessHtmlRequest(HttpHandlerContext context)
		{
			m_Context = context.Context;

			if (context.Request["pims"] != null)
				context.Context.Response.Redirect("http://pims.be.bvdep.net/PIMS/MainDimsRecord.asp?SelectedRecordId=" + context.Request["pims"], true);

			base.ProcessHtmlRequest(context);
		}
	}
}